package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.WxUtils;
import io.renren.modules.shop.entity.WxEntity;
import io.renren.modules.shop.entity.WxUser;
import io.renren.modules.shop.service.WxUserService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.service.SysUserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("wx_user")
@Log4j2
@Api(value = "微信用户controller", tags = {("微信用户接口")})
public class WxUserController extends AbstractController {
    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private SysUserTokenService sysUserTokenService;



    @ApiOperation(value = "微信登录")
    @PostMapping("login")
    public R register(@ApiParam("微信登录码") @RequestParam("jsCode") String jsCode) {
        WxUser wxUser = wxUserService.login(jsCode);
        log.info(wxUser);
        //生成token
        R token;
        if (wxUser == null) {
            return R.error("用户被冻结或账号错误");
        }
        token = sysUserTokenService.createToken(wxUser.getId());
        return token;
    }

    @ApiOperation(value = "登录后修改昵称")
    @PostMapping("addNikeName")
    public R addNikeName(@ApiParam("微信昵称") @RequestParam String nikeName) {
        try {
            WxUser wxUser = new WxUser();
            wxUser.setOpenid(getOpenId());
            WxUser user = wxUserService.getOne(new QueryWrapper<>(wxUser));
            user.setNickName(nikeName);
            wxUserService.updateById(user);
        } catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @ApiOperation(value = "查询分页")
    @PostMapping("page")
    public R getPage(@ApiParam("wx实体类") @RequestBody WxUser wxUser, @ApiParam("每页数量") @RequestParam Integer size,@ApiParam("页数")  @RequestParam Integer page) {
        IPage<WxUser> iPage = new Page<>(page, size);
        IPage<WxUser> goodsCouponIPage = wxUserService.page(iPage, new QueryWrapper<>(wxUser));
        PageUtils pageResult = new PageUtils(goodsCouponIPage);
        return R.ok().put("page", pageResult);
    }


    @ApiOperation(value = "查询所有数据")
    @PostMapping("list")
    public R getList(@ApiParam("wx实体类") @RequestBody WxUser wxUser) {
        List<WxUser> list = wxUserService.list(new QueryWrapper<>(wxUser));
        return R.ok().put("list", list);
    }

    @ApiOperation(value = "根据ids删除记录")
    @PostMapping("delete")
    public R del(@ApiParam("wx用户id") @RequestParam Integer[] ids) {
        boolean flag = wxUserService.removeByIds(Arrays.asList(ids));
        return R.ok().put("flag", flag);
    }

    @ApiOperation(value = "修改用户状态")
    @GetMapping("changeStatus")
    public R changeStatus(  @RequestParam Integer id) {
        WxUser wxUser = wxUserService.getById(id);
        if (wxUser.getStatus() == 1) {
            wxUser.setStatus(0);
        } else {
            wxUser.setStatus(1);
        }
        wxUserService.updateById(wxUser);
        return R.ok();
    }
}
